runOnStartup(async runtime => {

    runtime.objects.Door.addEventListener("instancecreate", (e) => {
        lib.EventDoorOpenAnaClose.AddDoorTargetMask(e)
        runtime.addEventListener("tick", () => {

            lib.EventDoorOpenAnaClose.PLayerOpenDoorAndCloseDoor()
        })
    })
})


lib.EventDoorOpenAnaClose = {


    AddDoorTargetMask: function (e) {
        /**@type {I3DShapeInstance} */
        var instance = e.instance
        /**@type {IRuntime} */
        var r = runtime
        var doorLayer = instance.layer.name
        var mask = r.objects.DoorMask.createInstance(doorLayer, instance.x, instance.y, true)
        instance.addChild(mask)
        mask.isVisible = true

    },


    PLayerOpenDoorAndCloseDoor: function () {

        var key = "f"
        var pl = runtime.objects.Pl?.getFirstInstance()

        lib.addEventListener(`_${key}_press`, () => {
            for (let doorMask of runtime.objects.DoorMask.instances()) {
                if (pl.testOverlap(doorMask)) {
                    var getDodor = doorMask.getParent()
                    if (getDodor == null) return
                    var CurrentState = getDodor.instVars.CurrentState
                    var OpenAn = getDodor.instVars.OpenAngel
                    var CloseAn = getDodor.instVars.CloseAngel
                    if (CurrentState == "Close") {
                        lib.ScreenHint.Hint("open", 2000)
                        setTimeout(() => {
                            //getDodor.angle = 0
                            //var lerpAngel = lib.c.call("Get_Lerp", getDodor.angle, OpenAn, 0.03)
                            // console.log(lerpAngel)
                            lib.c.call("_set_Door_Angel", getDodor.uid, OpenAn)
                            getDodor.instVars.CurrentState = "Open"
                        }, 200);
                    }
                    if (CurrentState == "Open") {
                        lib.ScreenHint.Hint("close", 2000)
                        setTimeout(() => {
                            //getDodor.angle = 90
                            lib.c.call("_set_Door_Angel", getDodor.uid, CloseAn)
                            getDodor.instVars.CurrentState = "Close"
                        }, 200)

                    }
                }
            }


        })

    }
}